#include <mqueue.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  int flags, opt;
  mode_t perms;
  mqd_t mqd;
  struct mq_attr attr, *attrp;

  attrp = NULL;
  attr.mq_maxmsg = 50;
  attr.mq_msgsize = 2048;
  flags = O_RDWR;
  perms = S_IRUSR | S_IWUSR;

  //Parse command-line options
  while ((opt = getopt(argc, argv, "cm:s:x")) != -1) {
    switch (opt) {
    case 'c':
      flags |= O_CREAT;
      break;
    case 'm':
      attr.mq_maxmsg = atoi(optarg);
      attrp = &attr;
      break;
    case 's':
      attr.mq_msgsize = atoi(optarg);
      attrp = &attr;
      break;
    case 'x':
      flags |= O_EXCL;
      break;
    default:
      printf("Unknown options\n");
    }
    if (optind >= argc)
      printf("Wrong usage\n");
    mqd = mq_open(argv[optind], flags, perms, attrp);
    if (mqd == (mqd_t)-1)
      printf("Failed to open a mq\n");
    return 0;
  }
}
